package game.resource.loader.builder;

import java.io.IOException;
import java.io.InputStream;

import org.xml.sax.Attributes;

import common.Coordinate;
import common.Log;
import common.Rectangle;

import engine.filesystem.FileScanner;
import engine.filesystem.Resource_A;
import engine.video.blit.BlitMode_E;
import engine.video.blit.PixelBuffer;
import engine.video.blit.SpriteInfo;

import game.resource.SpriteResource;
import game.resource.loader.PixelBufferLoader;
import game.resource.loader.tagparser.ClipTagParser;
import game.resource.loader.tagparser.MaskTagParser;
import game.resource.loader.tagparser.OffsetTagParser;
import game.resource.loader.tagparser.SpriteTagParser;
import game.resource.loader.tagparser.TagParser_I;

/**
<!-- Sprite XML Specification -->

<!-- 

Tag		Attribute	Description
sprite				Indicates a sprite definition.
		name		The name of this sprite, used by lookup methods in the engine.
		imagefile	Source image file
		maskfile*	If present, the indicated file will be used as a mask rahter than generating one.

mask*				If present, indicates that this sprite uses a mask.
		color*		The index of the transparent pixel, if absent the engine assumes index 0.

offset*				If present, indicates that this sprite is to be rendered using an offset.
		x*			default 0
		y*			default 0

clip*				If present, indicates that this sprite should be clipped before rendering.
		x*			default 0 - source clip rectangle.x
		y*			default 0 - soruce clip rectangle.y
		width		default image.width
		height		default image.height
-->

<sprite name = "sprite name" imagefile= "image.png" maskfile="maskimage.png" >
	Anything between tags will be echoed to the log.
	<mask color = 0 />
	<offset x = 0 y = 0 />
	<clip x = 0 y = 0 width = 64 height = 64/>
</sprite>

**/

public class SpriteBuilder implements ResourceBuilder_I {
	public SpriteBuilder() {
		SpriteTag = new SpriteTagParser();
		MaskTag = new MaskTagParser();
		OffsetTag = new OffsetTagParser();
		ClipTag = new ClipTagParser();
		_TagParsers = new TagParser_I[] { SpriteTag, MaskTag, OffsetTag, ClipTag };
	}
	
	public SpriteTagParser SpriteTag;
	public MaskTagParser MaskTag;
	public OffsetTagParser OffsetTag;
	public ClipTagParser ClipTag;
	private TagParser_I[] _TagParsers;
	
	@Override
	public boolean pushTag(String tag, Attributes attributes) {
		for(TagParser_I parser : _TagParsers) {
			if(parser.parse(tag, attributes) == true) {
				return true;
			}
		}
		return false;
	}


	@Override
	public Resource_A getResource(FileScanner scanner) {
		String name = SpriteTag.Name;
		String imageFile = SpriteTag.ImageFile;
		String maskFile = SpriteTag.MaskFile;
		boolean generateMask = MaskTag.GenerateMask;
		byte maskColor = MaskTag.MaskColor;
		Coordinate offset = OffsetTag.Offset;
		Rectangle clipRect = ClipTag.ClipRect;
		PixelBuffer image = null;
		PixelBuffer mask = null;
		
		// Check name.
		if(name == null) {
			Log.warning("Error loading sprite : name not set");
			return null;
		}
		
		if(imageFile == null) {
			Log.warning("Error loading sprite : imagefile not set");
			return null;
		}
		image = PixelBufferLoader.load(imageFile, scanner);
		// Load Sprite Image
		if(image == null) {
			Log.warning("Error loading sprite : image data not loaded");
			return null;
		}
		
		// Load Mask Image
		if(maskFile != null) {
			mask = PixelBufferLoader.load(maskFile, scanner);
		} else if (generateMask == true) { // or generate the mask.
			mask = image.generateMaskBuffer(maskColor);
		}
		
		SpriteInfo spriteInfo = new SpriteInfo(image, mask, offset, clipRect);
		return new SpriteResource(name, spriteInfo);
	}
}